package se.webbzon.boltzmann.editor.factory.propertyframe;

import java.awt.GridLayout;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import se.webbzon.boltzmann.editor.component.dependencylist.DependencyComboBox;
import se.webbzon.boltzmann.editor.component.sequence.SequenceBuilderPanel;
import se.webbzon.boltzmann.editor.mvc.EditorModel;
import se.webbzon.boltzmann.editor.propertyframe.WorldObjectPropertyFrame;
import se.webbzon.boltzmann.game.object.action.SequenceAction;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;

public class SequenceActionPropertyFrameFactory implements PropertyFrameFactory {

	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public String getWorldObjectName() {
		return SequenceAction.worldObjectName;
	}

	@Override public WorldObjectPropertyFrame makePropertyFrame(EditorModel model,
																WorldObject instance) {
		return new PropertyFrameImpl(model, (SequenceAction) instance);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private static final class PropertyFrameImpl extends WorldObjectPropertyFrame {

		// The input fields for this property frame
		private final JTextField rangeInput;
		
		// The combo boxes of this property frame
		private final DependencyComboBox<WorldStaticObject> parentInput;
		
		// The check boxes of this property frame
		private final JCheckBox frontOnlyInput;
		
		// The color choosers of this property frame
		private final JColorChooser colorInput;
		
		// The sequence builder panel of this property frame
		private final SequenceBuilderPanel sequencePanel;
		
		/*============================================================
		Constructors
		============================================================*/
		
		public PropertyFrameImpl(EditorModel model, SequenceAction action) {
			super(model, action);
			
			// Create action panel
			JPanel actionPanel = new JPanel();
			actionPanel.setLayout(new BoxLayout(actionPanel,BoxLayout.Y_AXIS));
			
			// Create configuration panel
			JPanel configPanel = new JPanel();
			configPanel.setBorder(BorderFactory.createTitledBorder("Action Configuration:"));
			configPanel.setLayout(new GridLayout(3,2));
			
			configPanel.add(new JLabel("Action Range:"));
			rangeInput = new JTextField("" + action.getRange());
			configPanel.add(rangeInput);
			configPanel.add(new JLabel("Parent:"));
			parentInput = 
					new DependencyComboBox<WorldStaticObject>(	model,
																WorldStaticObject.class,
																action.getParent());
			configPanel.add(parentInput);
			configPanel.add(new JLabel("Front Only:"));
			frontOnlyInput = new JCheckBox();
			frontOnlyInput.setSelected(action.getFrontOnly());
			configPanel.add(frontOnlyInput);
			actionPanel.add(configPanel);
			
			// Create color panel
			JPanel colorPanel = new JPanel();
			colorPanel.setBorder(BorderFactory.createTitledBorder("Action Color:"));
			colorInput = new JColorChooser(action.getColor());
			colorPanel.add(colorInput);
			actionPanel.add(colorPanel);
			
			addPanel("Action",actionPanel);
			
			// Create sequence panel
			sequencePanel = new SequenceBuilderPanel(model,action.getBuilder());
			addPanel(sequencePanel.getName(),sequencePanel);
			
			initialize();
		}
		
		/*============================================================
		Public Methods
		============================================================*/

		@Override public boolean apply(Collection<String> errorMessages) {
			try {
				SequenceAction action = (SequenceAction) getInstance();
				final double range = Double.parseDouble(rangeInput.getText());
				action.setColor(colorInput.getColor());
				action.setFrontOnly(frontOnlyInput.isSelected());
				action.setRange(range);
				action.setParent(parentInput.getInstance());
				return true;
			} catch (NumberFormatException e) {
				errorMessages.add(e.getMessage());
				return false;
			}
		}
	}

}
